
function _addZero(value) {
  return value > 10 ? value : '0' + value
}
function getIconDate(type) {
  const dt = new Date()
  switch (type) {
    case 'day':
      return _addZero(dt.getDate().toString())
    case 'month':
      return _addZero((dt.getMonth() + 1).toString())
    case 'year':
      return dt.getFullYear().toString().substr(2)
  }
}
function getNowDate(type) {
  const dt = new Date()
  const year = dt.getFullYear() + ''
  const month = (dt.getMonth() + 1 + '')
  const day = (dt.getDate() + '')
  switch (type) {
    case 'day':
      return `${year}-${month}-${day}`
    case 'month':
      return `${year}-${month}`
    case 'year':
      return `${year}`
  }
}
function formatDate(data, type) {
  const arr = data.split('-')
  switch (type) {
    case 'day':
      return `${arr[0]}年${arr[1]}月${arr[2]}日`
    case 'month':
      return `${arr[0]}年${arr[1]}月`
    case 'year':
      return `${arr[0]}年`
  }
}
function mapformatData(data, key, type) {
  return data.map((item) => {
    item[key] = formatDate(item[key], type)
    return item
  })
}
function formarUserValue(value) {
  const len = value.length
  if (len < 4) {
    return
  }
  if (len <= 4) {
    return value
  }
  let patten = null
  let arr = []
  if (len > 4 && len <= 6) {
    patten = /(\d{4})(\d{2})/
    value = value.substr(0, 6).replace(patten, '$1-$2')
    arr = Array.from(value).filter((e, i) => {
      if (i === 5 && e === '0') {
        return false
      }
      return true
    })
  }
  if (len > 6 && len <= 8) {
    patten = /(\d{4})(\d{2})(\d{2})/
    value = value.substr(0, 8).replace(patten, '$1-$2-$3')
    arr = Array.from(value).filter((e, i) => {
      if ((i === 5 || i === 8) && e === '0') {
        return false
      }
      return true
    })
  }
  return arr.toString().replace(/,/g, '')
}

export {
  getIconDate,
  getNowDate,
  formatDate,
  mapformatData,
  formarUserValue
}
